<template>
  <div class="container">
    <el-dialog
      :title="`${title}（${total}条）`"
      :visible.sync="value"
      width="55%"
      top="0"
      :before-close="handleClose"
      custom-class="dialogStyle"
    >
      <div class="seach">
        <div>
          <span class="title">群成员ID:</span>
          <el-input v-model="search.user_id" placeholder="请输入内容" size="medium" />
        </div>

        <div>
          <span class="title">红包类型：</span>
          <el-select v-model="search.type" placeholder="请选择" size="medium">
            <el-option
              v-for="item in typeList"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            />
          </el-select>
        </div>

        <div>
          <span class="title">创建时间：</span>
          <el-date-picker
            v-model="time"
            size="medium"
            type="daterange"
            value-format="yyyy-MM-dd"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            :clearable="false"
            :picker-options="pickerOptions"
            @change="selectTime"
          />
        </div>

        <div>
          <el-button size="small" type="primary" @click="init">重置</el-button>
          <el-button size="small" type="primary" @click="getList">搜索</el-button>
        </div>
      </div>
      <div class="content">
        <el-table
          v-loading="loading"
          :data="list"
          :header-cell-style="{'color':'#000'}"
          style="width: 100%"
        >
          <el-table-column
            label="成员ID"
            width="100"
            align="center"
          >
            <template slot-scope="scope">
              <span v-if="scope.row.user">
                {{ scope.row.user.id }}
              </span>
              <span v-else>-</span>
            </template>
          </el-table-column>
          <el-table-column
            label="头像"
            align="center"
            width="140"
          >
            <template slot-scope="scope">
              <div v-if="scope.row.user">
                <img :src="scope.row.user.avatar" alt="" style="width:50px;height:50px;object-fit:cover;">
              </div>
              <span v-else>-</span>
            </template>
          </el-table-column>
          <el-table-column
            label="昵称"
            align="center"
            width="140"
          >
            <template slot-scope="scope">
              {{ scope.row.user && scope.row.user.nickname }}
            </template>
          </el-table-column>
          <el-table-column
            label="性别"
            align="center"
            width="140"
          >
            <template slot-scope="scope">
              {{ scope.row.user && gender[scope.row.user.gender] }}
            </template>
          </el-table-column>
          <el-table-column
            label="红包金额"
            align="center"
            width="140"
          >
            <template slot-scope="scope">
              {{ scope.row.money }}
            </template>
          </el-table-column>
          <el-table-column
            label="红包数量"
            align="center"
            width="140"
          >
            <template slot-scope="scope">
              {{ scope.row.num }}
            </template>
          </el-table-column>
          <el-table-column
            label="创建时间"
            align="center"
            width="200"
          >
            <template slot-scope="scope">
              {{ scope.row.time }}
            </template>
          </el-table-column>
        </el-table>
        <div class="pagination">
          <el-pagination
            layout="prev, pager, next"
            :page-size="limit"
            :total="total"
            @current-change="handleCurrentChange"
          />
        </div>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  props: {
    value: {
      type: Boolean,
      default: false
    },
    title: {
      type: String,
      default: '群成员'
    },
    groupId: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      loading: false,
      list: [],
      page: 1,
      limit: 7,
      total: 0,
      time: '',

      role: { 'Owner': '群主', 'Member': '群成员', 'Admin': '群管理员' },
      gender: ['未知', '男', '女'],

      search: {
        type: 1,
        user_id: ''
      },
      pickerOptions: {
        disabledDate(time) {
          return time.getTime() > Date.now()
        }
      },
      typeList: [{ id: 1, name: '发红包' }, { id: 2, name: '抢红包' }]
    }
  },
  watch: {
    groupId(e) {
      this.init()
    }
  },
  methods: {
    // 关闭弹窗
    handleClose() {
      this.$emit('input', false)
    },

    // 选择时间
    selectTime(e) {
      this.search.start_time = e[0]
      this.search.end_time = e[1]
    },

    init() {
      this.page = 1
      this.limit = 7
      this.total = 0
      this.list = []
      this.search = { type: 1, user_id: '' }
      this.time = ''
      this.getList()
    },

    // 分页
    handleCurrentChange(e) {
      this.page = e
      this.getList()
    },

    // 获取群红包记录
    getList() {
      this.loading = true
      this.$request.post('/admin/group/user/red', {
        ...this.search,
        group_id: this.groupId,
        limit: this.limit,
        page: this.page
      }).then(res => {
        this.loading = false
        if (res.code == 200) {
          this.list = res.data.data.map(item => {
            const d = {}
            if (this.search.type == 1) {
              d.user = item.send_user
              d.money = item.total_money
              d.num = item.total_num
              d.time = item.created_at
            } else {
              d.user = item.user
              d.money = item.money
              d.num = 1
              d.time = item.created_at
            }
            return d
          })
          this.total = res.data.total
        }
      }).catch()
    }

  }
}
</script>

<style scoped>
  .container /deep/ .dialogStyle{
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
  }
  .container /deep/ .el-dialog__body {
    padding: 20px 20px 0;
  }
</style>
<style lang="scss" scoped>
    .seach{
    width: 100%;
    height: auto;
    padding: 10px;
    background-color: #fff;
    display: flex;
    justify-content: space-between;

    &>div{
      width: calc(100% / 4 - 20px);
      display: flex;
      align-items: center;

      &:nth-child(3){
        width: calc(100% / 4 + 80px);
      }
      &:nth-child(4){
        width: calc(100% / 4 - 100px);
      }
      &>.title{
        display: inline-block;
        width: 100px;
        font-size: 14px;
      }
    }
  }
  .content{
    width: 100%;
  }
  .btn{
    &>span{
      display: inline-block;
      color: #1989fa;
      text-align: center;
      cursor: pointer;
    }
  }

  .look{
    width: 100%;
    font-size: 14px;
    color: #1989fa;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    &>span{ margin-left: 10px; }
  }

  .dialog-footer{
    width: 100%;
    display: flex;
    align-items: center;
    justify-content: flex-end;
    &>div{
      margin-left: 30px;
      &>span{
        font-size: 16px;
      }
    }
  }

  .pagination{
    width: 100%;
    height: 80px;
    display: flex;
    align-items: center;
    justify-content: center;
    background-color: #fff;
  }
</style>
